package com.ueboot.weishop.admin.controller.setting;

import com.ueboot.core.http.response.Response;
import com.ueboot.weishop.admin.controller.setting.vo.BrandFindReq;
import com.ueboot.weishop.admin.controller.setting.vo.BrandFindResp;
import com.ueboot.weishop.admin.controller.setting.vo.BrandReq;
import com.ueboot.weishop.admin.controller.setting.vo.BrandResp;
import com.ueboot.weishop.entity.setting.BrandEntity;
import com.ueboot.weishop.entity.setting.CategoryEntity;
import com.ueboot.weishop.service.setting.BrandService;
import com.ueboot.weishop.service.setting.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.Assert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Neel on 2017/9/18.
 */
@Slf4j
@RestController
@RequestMapping(value = "/brands")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryService categoryService;

    @RequiresPermissions("brands:read")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Response<Page<BrandResp>> page(@PageableDefault(value = 15, sort = { "id" }, direction = Sort.Direction.DESC)
                                                      Pageable pageable, @RequestBody(required = false) BrandFindReq req) {
        Page<BrandEntity> entities = brandService.findBy(pageable);
        Page<BrandResp> body = entities.map(entity -> {
            BrandResp resp = new BrandResp();
            BeanUtils.copyProperties(entity, resp);
            if (entity.getCategory() == null) {
                return resp;
            }
            resp.setCategoryIds(entity.getCategory() == null ? new Long[]{} : (Long[]) ConvertUtils.convert(entity.getCategory().getFullPathId().split("/"), Long[].class));
            resp.setCategoryId(entity.getCategory() == null ? null : entity.getCategory().getId());
            resp.setCategory(entity.getCategory() == null ? null : entity.getCategory().getName());

            return resp;
        });
        return new Response<>(body);
    }


    @RequiresPermissions("brands:save")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Response<Void> save(@RequestBody BrandReq req) {
        BrandEntity entity = null;
        if (req.getId() == null) {
            entity = new BrandEntity();
        } else {
            entity = brandService.get(req.getId());
        }
        BeanUtils.copyProperties(req, entity);


        if (req.getCategoryIds() != null && req.getCategoryIds().length > 0) {
            CategoryEntity categoryEntity = categoryService.get(req.getCategoryIds()[req.getCategoryIds().length - 1]);
            entity.setCategory(categoryEntity);
        }
        brandService.save(entity);
        return new Response<>();
    }


    @RequiresPermissions("brands:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Response<Void> delete(Long[] id) {
        brandService.delete(id);
        return new Response<>();
    }

    @RequiresPermissions("brands:delete")
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public Response<Void> remove(Long id) {
        log.info("/brands/remove/{}", id);
        brandService.delete(id);
        return new Response<>();
    }


    @RequiresPermissions("brands:read")
    @RequestMapping(value = "/findByCategoryId", method = RequestMethod.POST)
    public Response<List<BrandFindResp>> findByCategoryId(@RequestBody BrandFindReq req) {
        Assert.notNull(req.getCategoryId());
        List<BrandEntity> list =brandService.findByCategoryId(req.getCategoryId());
        List<BrandFindResp> result = new ArrayList<>();
        list.forEach((brand)->{
            BrandFindResp resp = new BrandFindResp();
            BeanUtils.copyProperties(brand,resp);
            resp.setLabel(brand.getName());
            resp.setValue(brand.getId());
            result.add(resp);
        });
        return new Response<>(result);
    }


}
